package IEFYP.JunkCallFiltering;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import IEFYP.JunkCallFiltering.MyAdapter;





public class ReportedHistory extends Activity {
	private ListView historylist;
	private Spinner spn_type;
	private MyAdapter listItemAdapter ;
	private DBhelper u_data;
	private Button h_appeal; 
	private List<String> callno;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);
        findView();
        setListView();
        setSpinner();
        setOnClickListener();
        callno = new ArrayList<String>();
        
    }
    
    private void findView(){
    	historylist = (ListView) findViewById(R.id.historylist);
    	spn_type = (Spinner) findViewById(R.id.his_spn_type);
    	h_appeal = (Button) findViewById(R.id.h_appeal);
    	
    }
    
    private void setOnClickListener(){
    	  h_appeal.setOnClickListener(appeal);
    }
    
    private OnClickListener appeal = new OnClickListener()
    {
            public void onClick(View v)
            {
          	  DBhelper u_helper = new DBhelper(ReportedHistory.this, "userlist");
          	  u_helper.OpenDB();
              for(String call : callno){
          	  u_helper.delete(call);         	  
              }
              u_helper.close();
              setListView();
          	                                   
           }
       };
    
    private void setListView(){
    	List<Map<String, String>> ListItem = new ArrayList<Map<String,String>>();
    	//Log.e(this.toString(), "error: 1");
    /*	for(int i=0; i<10;i++){
    		String j= Integer.toString(i);
    		HashMap<String,String> map = new HashMap<String, String>();
    		map.put("No.","2345678"+j);
    		map.put("Type","Company"+j);
    		ListItem.add(map);
    	}
    	
    	*/
    	
    	u_data = new DBhelper(ReportedHistory.this,"userlist");
    	u_data.OpenDB();
        Cursor u_cursor = u_data.loadAll();
      
        if(u_cursor.moveToFirst()){
        	  int callnocol = u_cursor.getColumnIndex("call_no");
              int typecol = u_cursor.getColumnIndex("type");
        	
        	do{      	
        		HashMap<String,String> map = new HashMap<String, String>();
        		map.put("No.",u_cursor.getString(callnocol));
        		map.put("Type","Company"+u_cursor.getString(typecol));
        		ListItem.add(map);     		
        	}while(u_cursor.moveToNext());  
        	u_data.close();
 	
    	//Log.e(this.toString(), "error:2");
    	listItemAdapter = new MyAdapter(this,ListItem,
    			R.layout.blacklistcontent,
    			new String[]{"No.","Type"},
    			new int[]{R.id.callno,R.id.type},
    			false
    	);
    	//Log.e(this.toString(), "error:3 ");

    	historylist.setAdapter(listItemAdapter);
        }else{
			List<Map<String, String>> List = new ArrayList<Map<String,String>>();
    		HashMap<String,String> mapping = new HashMap<String, String>();
    		mapping.put("Notice","No Records");
    		List.add(mapping);
			SimpleAdapter  adapter = new SimpleAdapter(this,List,
    			R.layout.norecords,
    			new String[]{"Notice"},
    			new int[]{R.id.no_records});
			historylist.setAdapter(adapter);
		}
        
    	historylist.setOnItemClickListener(new OnItemClickListener() {

             @Override
             public void onItemClick(AdapterView<? > parent, View view, int position, long id) {      
             	
             	   CheckBox checkBox = (CheckBox) view.findViewById(R.id.b_select);                 
                   checkBox.toggle();
                   listItemAdapter .map.put(position, checkBox.isChecked());
                                     
                 // TODO Auto-generated method stub 
                 AbsoluteLayout lr=(AbsoluteLayout)view;
                TextView mText=(TextView)lr.getChildAt(1);
                String call = mText.getText().toString();
                
                if(!callno.contains(call) && checkBox.isChecked()){
                	callno.add(call);
                }
                if(callno.contains(call) && !checkBox.isChecked()){
                	callno.remove(call);
                }
                //CheckBox cb = (CheckBox) lr.getChildAt(2);
                 Toast.makeText(ReportedHistory.this, "You selected Item NO."+ position+"\nCall No.:"+mText.getText().toString(),//+"\n"+cb.toString(),
                         500).show();                  
             }
            });
  
    }
    
    private void setSpinner(){
        Resources res=getBaseContext().getResources();
        String[] types = res.getStringArray(R.array.types);
    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,types);
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	spn_type.setAdapter(adapter);
    	
    	/*spn_type.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
            public void onItemSelected(AdapterView adapterView, View view, int position, long id){
                Toast.makeText(ActionOnJunkCall.this, "You selected"+adapterView.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
            }
            public void onNothingSelected(AdapterView arg0) {
                Toast.makeText(ActionOnJunkCall.this, "No Selection", Toast.LENGTH_LONG).show();
            }
           
        });
         */ 	
    }
}
